<template>
  <div :class="className" :style="{height:height,width:width}" />
  <!-- <div v-else>
    No dream data!
  </div> -->
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { getBarchartOption } from '@/api/statistics'
const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      total: 0
    }
  },
  mounted() {
    this.$nextTick(() => {
      console.log('init barchart...')
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      getBarchartOption().then(response => {
        if (response.code === 20000 && response.data) {
          var option = response.data
          console.log('option: ')
          console.log(option)
          this.total = option.total
          this.chart.setOption({
            title: {
              text: 'Total dreams group by classification' ,
              subtext: 'Dream number: ' + this.total,
              left: 'left'
            },
            tooltip: {
              show: true,
              feature: {
                  mark: {show: true},
                  dataView: {show: true, readOnly: false},
                  magicType: {
                      show: true,
                      // type: ['pie', 'funnel']
                  },
                  restore: {show: true},
                  saveAsImage: {show: true}
              }
            },
            legend: option.legend,
            grid: {
              // top: 10,
              left: '2%',
              right: '2%',
              bottom: '3%',
              containLabel: true
            },
            xAxis: option.xAxis,
            yAxis: option.yAxis,
            series: option.series
          })
        } else if (!response.data){
          console.log('no dream found...')
        }
      })
    }
  }
}
</script>
